package com.bm.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bm.common.dto.DogkillDto;
import com.bm.common.util.Rutils;
import com.bm.common.vo.R;
import com.bm.server.dao.DogkillDao;
import com.bm.server.entity.Dogkill;
import com.bm.server.service.intf.DogkillService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.
 * |                     ______                     |
 * |                  .-"      "-.                  |
 * |                 /            \                 |
 * |     _          |              |          _     |
 * |    ( \         |,  .-.  .-.  ,|         / )    |
 * |     > "=._     | )(__/  \__)( |     _.=" <     |
 * |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |
 * |           "=._"(_     ^^     _)"_.="           |
 * |               "=\__|IIIIII|__/="               |
 * |              _.="| \IIIIII/ |"=._              |
 * |    _     _.="_.="\          /"=._"=._     _    |
 * |   ( \_.="_.="     `--------`     "=._"=._/ )   |
 * |    > _.="                            "=._ <    |
 * |   (_/                                    \_)   |
 * |                                                |
 * '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='
 * -----------------代码的搬运工-----------------
 *
 * @Date: 2022/07/21/11:58
 */
@Service

public class DogkillServiceImpl implements DogkillService {
    @Resource
    private DogkillDao dao;

    @Override
    public R save(DogkillDto dto) {
        if(dto!=null){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomid",dto.getRoomid());
            List<Dogkill> list = dao.selectList(queryWrapper);
            if(list.size()<7){
                Dogkill dogkill = new Dogkill();
                dogkill.setUid(dto.getUid());
                dogkill.setRoomid(dto.getRoomid());
                dao.insert(dogkill);
                return Rutils.ok("进入房间成功");
            }
        }
        return Rutils.fail("进入房间失败，人数已满");
    }

    @Override
    public R countuid(int roomid) {
        if(roomid>0){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomid",roomid);
            return Rutils.ok(dao.selectList(queryWrapper).size());
        }
        return Rutils.fail("房间不存在，请输入有效的房间号");
    }

    @Override
    public R deleteByUid(DogkillDto dto) {
        if(dto!=null){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid",dto.getUid());
            queryWrapper.eq("roomid",dto.getRoomid());
            dao.delete(queryWrapper);
            return Rutils.ok("用户退出房间成功");
        }
        return Rutils.fail();
    }
    //游戏开始分配身份
    @Override
    @Transactional
    public R gameStart(int roomid) {
        if(roomid>0){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomid",roomid);
            List<Dogkill> list = dao.selectList(queryWrapper);
            if(list.size()==7){
                String[] str = {"狼人","狼人","平民","平民","预言家","女巫","猎人"};
                int lenght = str.length;
                for (int i =1;i<lenght;i++){
                    int irandom =(int)(Math.random()*lenght);
                    String tmp = str[irandom];
                    str[irandom] = str[i];
                    str[i] = tmp;
                }
                for(int i =0;i<list.size();i++){
                    Dogkill dogkill1 = list.get(i);
                    dao.update(dogkill1.getUid(),str[i]);
                }
                if(dao.updatenvwu(roomid)>0){
                    return Rutils.ok("身份分配成功，请点击查看身份");
                }
            }else {
                return Rutils.fail("人数不足，请等待人数");
            }

        }
        return null;
    }
    //狼人通过用户id杀人
    @Override
    public R wolfkillpeople(int uid) {
        if(uid>0){
            dao.updateflag(uid);
            return Rutils.ok("击杀成功");
        }
        return Rutils.fail("击杀失败");
    }
    //点击查看自己的身份
    @Override
    public R selectByUid(int uid) {
        if(uid>0){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid",uid);
            return Rutils.ok(dao.selectOne(queryWrapper));
        }
        return Rutils.fail("查看失败，请重新查看");
    }
    //女巫救人接口
    @Override
    public R nvwusave(int roomid,String save) {
        if(roomid>0 || save!=null){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomid",roomid);
            queryWrapper.eq("name","女巫");
            Dogkill dogkill = dao.selectOne(queryWrapper);
            if(dogkill.getJieyao()==1){
                QueryWrapper<Dogkill> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("roomid",roomid);
                queryWrapper1.eq("flag",0);
                Dogkill dogkill1 = dao.selectOne(queryWrapper1);
                if(save.equals("救")){
                    if(dao.updateflagsave(dogkill1.getUid())>0){
                        dao.updatejieyao(dogkill.getUid());
                        return Rutils.ok("女巫选择救人，游戏继续");
                    }
                }else{
                    return Rutils.ok("女巫选择不救，游戏继续进行");
                }
            }else {
                return Rutils.ok("女巫没有解药，游戏继续进行");
            }
        }
        return Rutils.ok("女巫没有选择，游戏继续进行");
    }
    //女巫毒人接口
    @Override
    public R nvwukill(int roomid, String kill,int uid) {
        if(roomid>0 || kill!=null ||uid>0){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomid",roomid);

            queryWrapper.eq("name","女巫");
            Dogkill dogkill = dao.selectOne(queryWrapper);
            if(dogkill.getDuyao()==1){
                if(kill.equals("毒")){
                    if(dao.updateflag(uid)>0){
                        dao.updateduyao(dogkill.getUid());
                        return Rutils.ok("女巫选择毒人，游戏继续进行");
                    }
                }else{
                    return Rutils.ok("女巫选择不毒人，游戏继续进行");
                }
            }else {
                return Rutils.ok("女巫没有毒药，游戏继续进行");
            }
        }
        return Rutils.ok("女巫没有选择，游戏继续进行");
    }
    //天亮执行死亡接口
    @Override
    public R deleteByid(int roomid,int uid) {
       if(roomid>0){
           QueryWrapper queryWrapper = new QueryWrapper();
           queryWrapper.eq("roomid",roomid);
           queryWrapper.eq("flag",0);
           List<Dogkill> list = dao.selectList(queryWrapper);
           for (int i =0;i<list.size();i++){
               Dogkill dogkill = list.get(i);
               if(dogkill.getName().equals("猎人")){
                    dao.updateflagdead(uid);
               }
               dao.updateflagdead(dogkill.getUid());
           }
           return Rutils.ok("角色已死亡，游戏继续");
       }
        return Rutils.fail("角色没有死亡，发生异常");
    }
    //判断好人是否胜利
    @Override
    public R isgameoverhao(int roomid) {
        if(roomid>0){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomid",roomid);
            queryWrapper.eq("name","狼人");
            queryWrapper.eq("flag",1);
            List<Dogkill> list = dao.selectList(queryWrapper);
            if(list.size()==0){
                return Rutils.ok("游戏结束，好人胜利");
            }
        }
        return Rutils.fail("游戏没有结束，请继续游戏");
    }
    //判断狼人是否胜利
    @Override
    public R isgameoverhuai1(int roomid) {
        if(roomid>0){
            QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomid",roomid);
            queryWrapper.eq("name","平民");
            queryWrapper.eq("flag",1);
            List<Dogkill> list = dao.selectList(queryWrapper);
            if(list.size()==0){
                return Rutils.ok("游戏结束,狼人胜利");
            }
        }
        return Rutils.fail("游戏没有结束，请继续游戏");
    }

    @Override
    public R isgameoverhuai2(int roomid) {
       if(roomid>0){
           List<Dogkill> list = new ArrayList<>();
           QueryWrapper<Dogkill> queryWrapper = new QueryWrapper<>();
           queryWrapper.eq("roomid",roomid);
           queryWrapper.eq("name","预言家");
           queryWrapper.eq("flag",1);
           list.add(dao.selectOne(queryWrapper));
           QueryWrapper<Dogkill> queryWrapper1 = new QueryWrapper<>();
           queryWrapper1.eq("roomid",roomid);
           queryWrapper1.eq("name","女巫");
           queryWrapper1.eq("flag",1);
           list.add(dao.selectOne(queryWrapper1));
           QueryWrapper<Dogkill> queryWrapper2 = new QueryWrapper<>();
           queryWrapper2.eq("roomid",roomid);
           queryWrapper2.eq("name","猎人");
           queryWrapper2.eq("flag",1);
           list.add(dao.selectOne(queryWrapper2));
           if(list.size()==0){
               return Rutils.ok("游戏结束，狼人胜利");
           }
       }
        return Rutils.fail("游戏没有结束，请继续游戏");
    }
    //投票阶段
    @Override
    public R toupiao(int roomid, int uid) {
        if(roomid>0 || uid>0){

            if(dao.updateNums(roomid, uid)>0){
                return Rutils.ok("投票成功");
            }
        }
        return Rutils.fail("投票失败!");
    }
}

